<?php
namespace app\libs;

use think\Exception;
use think\facade\Config;

/**
 * 中午打印机
 * Class Printer
 * @package app\libs
 */
class Printer
{
    protected $config;
    protected $device_id;
    protected $device_secret;
    protected $printer;

    public function __construct($device_id,$device_secret)
    {
        $this->config = Config::get('system.zhongwuyun');
        $this->device_id = $device_id;
        $this->device_secret = $device_secret;

        // 实例化打印机
        $rpc = new \zhongwu\protocol\RpcClient($this->config['appid'], $this->config['appsecret'], $this->config['apiurl']);
        $this->printer = new \zhongwu\Printer($rpc);
    }

    /**
     * 获取返回数据
     * @param $ret
     * @return array
     */
    public function getReturnData($ret)
    {
        if($ret->errNum==0){
            return ['code'=>1,'data'=>$ret->retData];
        }else{
            return ['code'=>0,'msg'=>$ret->errMsg];
        }
    }

    /**
     * 打印
     * @param $print_data
     * @return array
     * @throws \zhongwu\exceptions\BusinessException
     */
    public function onPrint($print_data)
    {
        try {
            $ret = $this->printer->set_args($this->device_id, $this->device_secret)->cloud_print(trim($print_data));
            return $this->getReturnData($ret);
        } catch (Exception $e) {
            return ['code'=>0,'msg'=>$e->getMessage()];
        }
    }

    /**
     * 设置音量
     * @param $sound
     * @return array
     * @throws \zhongwu\exceptions\BusinessException
     */
    public function setSound($sound)
    {
        try {
            $ret = $this->printer->set_args($this->device_id, $this->device_secret)->set_sound($sound);
            return $this->getReturnData($ret);
        } catch (Exception $e) {
            return ['code'=>0,'msg'=>$e->getMessage()];
        }
    }

    /**
     * 设置logo
     * @param $logo
     * @return array
     * @throws \Exception
     */
    public function setLogo($logo)
    {
        try {
            $ret = $this->printer->set_args($this->device_id, $this->device_secret)->set_logo($logo);
            return $this->getReturnData($ret);
        } catch (Exception $e) {
            return ['code'=>0,'msg'=>$e->getMessage()];
        }
    }

    /**
     * 获取状态
     * @return array
     * @throws \Exception
     */
    public function getStatus()
    {
        try {
            $ret = $this->printer->set_args($this->device_id, $this->device_secret)->get_status();
            return $this->getReturnData($ret);
        } catch (Exception $e) {
            return ['code'=>0,'msg'=>$e->getMessage()];
        }
    }

    /**
     * 清空队列
     * @return array
     * @throws \zhongwu\exceptions\BusinessException
     */
    public function emptyQueue()
    {
        try {
            $ret = $this->printer->set_args($this->device_id, $this->device_secret)->empty_print_queue();
            return $this->getReturnData($ret);
        } catch (Exception $e) {
            return ['code'=>0,'msg'=>$e->getMessage()];
        }
    }
}